﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class EditUsers : System.Web.UI.Page
{
    public ProxyWrapper.UserProxy.UserDetails UserObject { get; set; }
    public bool isEmployee = false;
    public bool isAddEmployee = false;
    internal string roleId = "0";
    public DataTable Data { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {

            if (Session["userObject"] == null)
            {
                Response.Redirect("Default.aspx", false);
            }
            else
            {
                if (Request.QueryString["roleid"] != null)
                {
                    roleId = Request.QueryString["roleid"].ToString();

                }

                UserObject = ((ProxyWrapper.UserProxy.UserDetails)Session["UserObject"]);
                if (UserObject.RoleId == 1)
                {
                    ProxyWrapper.UserServiceWrapper _usr = new ProxyWrapper.UserServiceWrapper();
                    Data = new DataTable();
                    Data.Columns.Add("UserId");
                    Data.Columns.Add("AppUserName");
                    Data.Columns.Add("EmailAdddress");
                    Data.Columns.Add("FirstName");
                    Data.Columns.Add("LastName");
                    Data.Columns.Add("PhoneNumber");
                    Data.Columns.Add("Password");
                    Data.Columns.Add("ExpiryDate");
                    Data.Columns.Add("DeviceCount");
                    Data.Columns.Add("UserType");
                    Data.Columns.Add("ClientEmail");
                    Data.Columns.Add("RoleId");
                    DataRow _dr = null;
                    if (Request.QueryString["id"] != null)
                    {
                        DataRow[] dr = _usr.GetAccountUsers(roleId).Tables[0].Select("UserId=" + Request.QueryString["id"].ToString());
                        for (int i = 0; i < dr.Length; i++)
                        {
                            _dr = Data.NewRow();
                            _dr[0] = dr[0][0];
                            _dr[1] = dr[0][1];
                            _dr[2] = dr[0][2];
                            _dr[3] = dr[0][3];
                            _dr[4] = dr[0][4];
                            _dr[5] = dr[0][5];
                            _dr[6] = dr[0][6];
                            _dr[7] = dr[0][7];
                            _dr[8] = dr[0][8];
                            _dr[9] = dr[0][9];
                            _dr[10] = dr[0][10];
                            _dr[11] = dr[0][11];
                            Data.Rows.Add(_dr);
                            if (dr[0][11].ToString().Equals("2"))
                                isEmployee = true;
                        }
                    }
                    else if (Request.QueryString["addnew"] != null)
                    {
                        _dr = Data.NewRow();
                        _dr[0] = "0";
                        _dr[1] = string.Empty;
                        _dr[2] = string.Empty;
                        _dr[3] = string.Empty;
                        _dr[4] = string.Empty;
                        _dr[5] = string.Empty;
                        _dr[6] = string.Empty;
                        _dr[7] = string.Empty;
                        _dr[8] = string.Empty;
                        _dr[9] = "Employee";
                        _dr[10] = string.Empty;
                        _dr[11] = "2";
                        Data.Rows.Add(_dr);
                        isAddEmployee = true;
                    }
                }
                else
                {
                    Response.Redirect("AccessDenied.aspx");
                }
            }

        }
        catch (Exception ex)
        { }
    }
    public string GetNav()
    {
        if (isAddEmployee)
            return "Add New Employee";
        if (isEmployee)
            return "Edit Employee";
        else
            return "Edit User";
    }

    public string GetButtonText()
    {
        if (isAddEmployee)
            return "Add Employee";
        if (isEmployee)
            return "Update Employee";
        else
            return "Update User";
    }
    public string EnableForEmployee()
    {
        if (isAddEmployee || isEmployee)
            return "";
        else
            return "readonly='readonly' disabled='disabled'";
    }


}